package com.jcfk.eam.service.mould;

import com.jcfk.eam.domain.dto.mould.EamMouldTaskInfoDTO;
import com.jcfk.eam.domain.po.main.EamAssetInfo;
import com.jcfk.eam.domain.po.mould.EamMouldOutinboundRecord;
import com.jcfk.eam.domain.po.mould.EamMouldTaskInfoDetail;
import com.jcfk.eam.domain.po.mould.EamMouldWorkRecord;
import com.jcfk.eam.domain.query.mould.EamMouldOutinboundRecordQuery;
import com.jcfk.eam.domain.dto.mould.EamMouldOutinboundRecordDTO;
import com.jcfk.eam.dao.mould.EamMouldOutinboundRecordDao;
import com.jcfk.common.business.service.BaseService;
import com.jcfk.common.web.domain.DataPage;
import com.jcfk.common.web.domain.RetResult;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jcfk.eam.service.main.EamAssetInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Transactional;
import com.jcfk.common.core.utils.BeanUtils;
import cn.hutool.core.util.StrUtil;
import com.jcfk.common.security.utils.SecurityUtils;
import com.alibaba.druid.util.StringUtils;

import java.util.List;
import java.util.ArrayList;
import java.util.stream.Collectors;
import com.jcfk.common.core.utils.IdUtils;
import static com.jcfk.common.core.utils.BeanUtils.convertList;

import java.util.Date;

/**
 * 模具出入库记录表
 *
 * @author awei
 * @email 522891237@gqq.com
 * @date 2025-08-01
 */
@Service
public class EamMouldOutinboundRecordService extends BaseService<EamMouldOutinboundRecordDao,EamMouldOutinboundRecord> {

    @Autowired
    private EamAssetInfoService eamAssetInfoService;
    /**
     * EamMouldOutinboundRecord分页查询
     *
     * @param page  分页设置
     * @param query 查询条件
     * @return 分页查询结果
     */
    public DataPage<EamMouldOutinboundRecordDTO> queryByPage(DataPage<EamMouldOutinboundRecordDTO> page, EamMouldOutinboundRecordQuery query) {
        // 分页查询
        page = this.baseMapper.queryByPage(page, query);
        return page;
    }


        /**
     * 判断EamMouldOutinboundRecord是已经存在
     *
     * @param dto
     * @return
     */
    public RetResult<String> isValid(EamMouldOutinboundRecordDTO dto) {
        QueryWrapper<EamMouldOutinboundRecord> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("deleted", 0);
                                                                                
        return RetResult.ok();
    }

    /**
     * 插入模具出入库记录表
     *
     * @param dto 插入对象
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public void insert(EamMouldOutinboundRecordDTO dto) {

        // 保存
        EamMouldOutinboundRecord item = new EamMouldOutinboundRecord();
        BeanUtils.copyProperties(dto, item);
        this.setDefaultValue(item);

        super.save(item);
    }

    /**
     * 更新模具出入库记录表
     *
     * @param dto 更新对象
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public void update(EamMouldOutinboundRecordDTO dto) {

        // 保存
        EamMouldOutinboundRecord item = new EamMouldOutinboundRecord();
        BeanUtils.copyProperties(dto, item);

        // 默认数据
        this.setDefaultValue(item);

        super.updateById(item);
    }

    /**
     * 根据批量主键删除模具出入库记录表(逻辑删除)
     *
     * @param dto
     * @return bool
     */
    @Transactional(rollbackFor = Exception.class)
    public Boolean logicDeleteList(EamMouldOutinboundRecordDTO dto) {

        EamMouldOutinboundRecord item = new EamMouldOutinboundRecord();

        this.setDefaultValue(item);
        item.setVersion(dto.getVersion());
        item.setDeleted(1);

        QueryWrapper<EamMouldOutinboundRecord> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("id", dto.getIds());

        return this.update(item, queryWrapper);
    }

    /**
     * 设置添加默认值
     *
     * @param item 添加对象
     */
    public void setDefaultValue(EamMouldOutinboundRecord item) {
        if (StrUtil.isEmpty(item.getCreatedUserId())) {
            item.setCreatedUserId(SecurityUtils.getUserId());
            item.setCreateTime(new Date());
            item.setVersion(1L);
                item.setDeleted(0);
                                                                                                                                                                                                    item.setCreatedUserName(SecurityUtils.getUserName());
                                                                                }
        item.setLastUpdatedUserId(SecurityUtils.getUserId());
        item.setLastUpdateTime(new Date());

                                                                                item.setLastUpdatedUserName(SecurityUtils.getUserName());
            
    }

    /**
     * 根据主键获取模具出入库记录表信息
     *
     * @param id 主键
     * @return StandardCodeDTO
     */
    public EamMouldOutinboundRecordDTO get(String id) {
        // 查询数据
        EamMouldOutinboundRecord item = this.baseMapper.selectById(id);
            EamMouldOutinboundRecordDTO dto = new EamMouldOutinboundRecordDTO();
        try {
            BeanUtils.copyProperties(item, dto);
        } catch (RuntimeException e) {
            throw e;
        }

        return dto;
    }

    

    /**
     * 根据条件获取单表信息列表
     *
     * @param query 查询条件
     * @return List<EamMouldOutinboundRecordDTO>
     */
    public List<EamMouldOutinboundRecordDTO> getList(EamMouldOutinboundRecordQuery query) {
        // 查询条件
        QueryWrapper<EamMouldOutinboundRecord> qw = new QueryWrapper<>();
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                // 查询数据
        List<EamMouldOutinboundRecord> list = super.list(qw);

        return convertList(list, EamMouldOutinboundRecordDTO.class);
    }

    /**
     * 根据主键获取模具出入库记录表信息
     *
     * @param id 主键
     * @return StandardCodeDTO
     */
    public void deleteItem(String id) {
            EamMouldOutinboundRecord item = getById(id);
        this.setDefaultValue(item);
        item.setDeleted(1);

        this.updateById(item);
        }

    public int insertBatch(List<EamMouldTaskInfoDetail> detailList, int i, EamMouldTaskInfoDTO eamMouldTaskInfoDTO) {

        List<EamMouldOutinboundRecord> savelist = new ArrayList<>();
        detailList.forEach(itemOne -> {
            // 保存
            EamMouldOutinboundRecord item = new EamMouldOutinboundRecord();
            this.setDefaultValue(item);
            item.setMouldCode(itemOne.getMouldCode());
            item.setMouldName(itemOne.getMouldName());
            item.setLocationName(eamMouldTaskInfoDTO.getLocationName());
            item.setLocationId(eamMouldTaskInfoDTO.getLocationId());
            item.setOutinboundType(i);
            item.setPassStatus(eamMouldTaskInfoDTO.getPassStatus());
            item.setMouldType(itemOne.getMouldType());
            item.setOutinboundTime(new Date());
            item.setSubmitStatus(1);
            item.setTaskDetailId(itemOne.getId());
            savelist.add(item);
        });
        // 保存
        if (savelist.size() > 0) super.saveBatch(savelist);
        return 1;
    }
}

